﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.SystemSetting;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.SystemSetting
{
    /// <summary>
    /// 角色用户相关
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class JueSeUserController : Controller
    {
        private readonly IJueSeUser _IJueSeUser;
        private readonly ICustomJWTService _ICustomJWTService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private static readonly ILog log = LogManager.GetLogger(typeof(IndexController));

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IJueSeUser"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public JueSeUserController(IJueSeUser IJueSeUser, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IJueSeUser = IJueSeUser;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }
        /// <summary>
        /// 查询角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelJueSeAll()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.GetSelJueSeAll());
        }
        /// <summary>
        /// 根据角色ID查询用户和权限
        /// </summary>
        /// <param name="strJuseId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelYongHu(string strJuseId)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.GetSelYongHu(strJuseId));
        }
        /// <summary>
        /// 查询全部权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelQuanXian()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IJueSeUser.GetSelQuanXian(strYuanQu));
        }
        /// <summary>
        /// 角色新增
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostJueSeAdd(JueSeClass jueSe)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            var strXiTongMingCheng = token.Claims.FirstOrDefault(t => t.Type == "DengLuXiTong")?.Value;
            return Ok(_IJueSeUser.PostJueSeAdd(jueSe, strYuanQuQianZhui, strXiTongMingCheng));
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutJueSeUpdate(JueSeClass jueSe)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PutJueSeUpdate(jueSe));
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="jueSe"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteJueSeDelete(JueSeClass jueSe)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.DeleteJueSeDelete(jueSe));
        }
        /// <summary>
        /// 用户新增
        /// </summary>
        /// <param name="userClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostUserAdd(UserClass userClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IJueSeUser.PostUserAdd(userClass, strYuanQuQianZhui));
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="userClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutUserUpdate(UserClass userClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PutUserUpdate(userClass));
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="yongHu"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteUserDelete(YongHuClass yongHu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.DeleteUserDelete(yongHu));
        }
        /// <summary>
        /// 解除用户锁定
        /// </summary>
        /// <param name="yongHu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")] 
        public IActionResult PutJieChuSuoDing(YongHuClass yongHu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PutJieChuSuoDing(yongHu));
        }
        /// <summary>
        /// 搜索特定用户
        /// </summary>
        /// <param name="strUser"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelUser(string strUser)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.GetSelUser(strUser));
        }
        /// <summary>
        /// 角色保存权限
        /// </summary>
        /// <param name="SaveJuSeQuanXianClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostSaveJuSeQuanXian(SaveJuSeQuanXianClass SaveJuSeQuanXianClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PostSaveJuSeQuanXian(SaveJuSeQuanXianClass));
        }
        /// <summary>
        /// 用户批量权限调整
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostUserZhuiJiaQuanXian(UserQuanXianClass userQuanXian)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PostUserZhuiJiaQuanXian(userQuanXian));
        }
        /// <summary>
        /// 用户角色顺序调整
        /// </summary>
        /// <param name="userJueSeShunXu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutUserJueSeTiaoZhengShunXu(UserJueSeShunXuClass userJueSeShunXu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IJueSeUser.PutUserJueSeTiaoZhengShunXu(userJueSeShunXu));
        }
    }
}
